﻿using GBT.ParamEntity;
using GBT.SystemBusinessCore;
using GBT.SystemEnties;
using SqlSugar;
using System.Linq;

namespace QueryDataLogic.Core
{
    public class UserStoredValueManage
    {
        BasicInfo bi = new BasicInfo();
        DbContext<UserStoredValue> dbContext = null;

        /// <summary>
        /// 获取充值记录
        /// </summary>
        /// <param name="userStoredValueRecord"></param>
        /// <param name="totalNumber"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public object GetUserStoredValueRecord(System_UserStoredValueRecord userStoredValueRecord, ref int totalNumber, ref int totalPage)
        {
            var dbName = bi.GetDBName((int)userStoredValueRecord.DistrictID);
            dbContext = new DbContext<UserStoredValue>(dbName);
            Manage_District mDistrict = new Manage_District();
            mDistrict.DistrictID = userStoredValueRecord.DistrictID;
            var district = bi.GetDistrictInfo(mDistrict);
            var db = dbContext.Db;
            var userStoredValueRecordList = db.Queryable<UserStoredValueRecord, MealUser, AddressBook, MealUser>((usvr, mu, ab, mu1) => new object[] {
                JoinType.Left, usvr.UserID == mu.UserID,
                JoinType.Left, mu.AddressBookID == ab.ID,
                JoinType.Left, usvr.Creator == mu1.UserID,
            })
            .Where((usvr, mu, ab, mu1) => mu.DistrictID == userStoredValueRecord.DistrictID)
            .WhereIF(userStoredValueRecord.UserID != null, (usvr, mu, ab, mu1) => usvr.UserID == userStoredValueRecord.UserID)
            .OrderBy((usvr, mu, ab, mu1) => usvr.CreationDate, OrderByType.Desc)
            .Select((usvr, mu, ab, mu1) => new
            {
                usvr.ID,
                usvr.UserID,
                ab.Number,
                ab.name,
                mu.nickName,
                usvr.Value,
                usvr.UserValue,
                usvr.Remark,
                CreatorNumber = mu1.Number,
                CreatorName = mu1.Name,
                usvr.CreationDate,
            }).ToPageList(userStoredValueRecord.PageIndex, userStoredValueRecord.PageSize, ref totalNumber, ref totalPage);

            var userStoredValueRecordListNew = userStoredValueRecordList.Select(t => new
            {
                t.ID,
                t.UserID,
                t.Number,
                t.name,
                t.nickName,
                t.Value,
                t.UserValue,
                t.Remark,
                t.CreatorNumber,
                t.CreatorName,
                t.CreationDate,
                district.DistrictNumber,
                district.DistrictName,
            }).ToList();

            return userStoredValueRecordListNew;
        }

        /// <summary>
        /// 获取消费记录
        /// </summary>
        /// <param name="userXpenseTracker"></param>
        /// <param name="totalNumber"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public object GetUserXpenseTracker(System_UserXpenseTracker userXpenseTracker, ref int totalNumber, ref int totalPage)
        {
            var dbName = bi.GetDBName((int)userXpenseTracker.DistrictID);
            dbContext = new DbContext<UserStoredValue>(dbName);
            Manage_District mDistrict = new Manage_District();
            mDistrict.DistrictID = userXpenseTracker.DistrictID;
            var district = bi.GetDistrictInfo(mDistrict);
            var db = dbContext.Db;
            var userXpenseTrackerList = db.Queryable<UserXpenseTracker, MealUser, DiningHall, AddressBook>((uxt, mu, dh, ab) => new object[] {
                JoinType.Left, uxt.UserID == mu.UserID,
                JoinType.Left, uxt.HallID == dh.HallId,
                JoinType.Left, mu.AddressBookID == ab.ID,
            })
            .Where((uxt, mu, dh, ab) => mu.DistrictID == userXpenseTracker.DistrictID)
            .WhereIF(userXpenseTracker.UserID != null, (uxt, mu, dh, ab) => uxt.UserID == userXpenseTracker.UserID)
            .WhereIF(userXpenseTracker.HallID != null, (uxt, mu, dh, ab) => uxt.HallID == userXpenseTracker.HallID)
            .OrderBy((uxt, mu, dh, ab) => uxt.CreationDate, OrderByType.Desc)
            .Select((uxt, mu, dh, ab) => new
            {
                uxt.ID,
                uxt.HallID,
                uxt.UserID,
                dh.HallNumber,
                dh.HallName,
                ab.Number,
                ab.name,
                mu.nickName,
                uxt.Value,
                uxt.UserValue,
                uxt.Remark,
                uxt.CreationDate
            }).ToPageList(userXpenseTracker.PageIndex, userXpenseTracker.PageSize, ref totalNumber, ref totalPage);

            var userXpenseTrackerListNew = userXpenseTrackerList.Select(t => new
            {
                t.ID,
                t.HallID,
                t.UserID,
                t.HallNumber,
                t.HallName,
                t.Number,
                t.name,
                t.nickName,
                t.Value,
                t.UserValue,
                t.Remark,
                t.CreationDate,
                district.DistrictNumber,
                district.DistrictName,
            }).ToList();

            return userXpenseTrackerListNew;
        }
    }
}
